package com.chushouya.product.dao.repository;

import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.product.dao.entity.TempEvaluateItemValueEntity;
import com.chushouya.product.dao.mapper.TempEvaluateItemValueEntityMapper;
import com.chushouya.product.dto.admin.tempevaluate.TempEvaluateItemValueQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Repository
public class TempEvaluateItemValueRepository extends BaseRepository<TempEvaluateItemValueEntityMapper, TempEvaluateItemValueEntity, Long> {
    
    public List<TempEvaluateItemValueEntity> selectList(TempEvaluateItemValueQuery query) {
        Example example = buildExample(query);
        return this.selectByExample(example);
    }

    public PageResult<TempEvaluateItemValueEntity> selectPageResult(TempEvaluateItemValueQuery query) {
        Example example = this.buildExample(query);
        return this.selectPage(query.getPageRequest(), example);
    }
    
    public TempEvaluateItemValueEntity selectOne(TempEvaluateItemValueQuery query) {
        Example example = buildExample(query);
        return this.selectFirstOneByExample(example);
    }

    public int deleteByTempItemId(Long tempItemId) {
        if (Objects.isNull(tempItemId)) {
            return 0;
        }
        Example example = new Example(TempEvaluateItemValueEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("tempItemId", tempItemId);
        return this.deleteByExample(example);
    }

    private Example buildExample(TempEvaluateItemValueQuery query) {
        Example example = new Example(TempEvaluateItemValueEntity.class);
        Example.Criteria criteria = example.createCriteria();
        if (Objects.nonNull(query.getTempItemValueId())) {
            criteria.andEqualTo("tempItemValueId", query.getTempItemValueId());
        }
        if (Objects.nonNull(query.getTempItemId())) {
            criteria.andEqualTo("tempItemId", query.getTempItemId());
        }
        if (Objects.nonNull(query.getValueId())) {
            criteria.andEqualTo("valueId", query.getValueId());
        }
        if (Strings.isNotEmpty(query.getValueName())) {
            criteria.andLike("valueName", "%" + query.getValueName() + "%");
        }
        if (Objects.nonNull(query.getCalcType())) {
            criteria.andEqualTo("calcType", query.getCalcType());
        }
        example.orderBy("sort").asc();
        return example;
    }

}
